package ilearn.seata.job.java8.buss.service;

import ilearn.seata.job.java8.buss.client.OrderServiceClilent;
import ilearn.seata.job.java8.buss.client.PointsServiceClient;
import ilearn.seata.job.java8.buss.client.entity.Order;
import ilearn.seata.job.java8.buss.client.entity.Points;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class BussinessService {
    @Resource
    private OrderServiceClilent orderServiceClilent;
    @Resource
    private PointsServiceClient pointsServiceClient;

    @GlobalTransactional(name = "busss-sale",timeoutMills = 120000,rollbackFor = Exception.class)
    public Order sale(String goods, String accId, String name, BigDecimal amt){
        Order order = orderServiceClilent.add(goods,accId,name,amt);
        if (amt.compareTo(new BigDecimal("0")) == 0){
            throw new RuntimeException("amt = 0");
        }
        pointsServiceClient.increaase(accId,amt.intValue());
        if (amt.compareTo(new BigDecimal("-1")) == 0 ){
            throw new RuntimeException("amt = -1");
        }
        return order;
    }
}
